package com.woniuxy.productservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.CoachState;
import com.woniuxy.core.status.CourseTypeStateEnum;
import com.woniuxy.core.status.ProductState;
import com.woniuxy.productclient.client.CoachFeignClient;
import com.woniuxy.productclient.client.CourseFeignClient;
import com.woniuxy.productclient.client.dto.CoachInfoDto;
import com.woniuxy.productclient.client.dto.CourseTypeDto;
import com.woniuxy.productdao.mapper.AloneCourseMapper;
import com.woniuxy.productdao.model.AloneCourse;
import com.woniuxy.productmodel.dto.AloneCourseProductDto;
import com.woniuxy.productmodel.form.AddAlongCourseProductForm;
import com.woniuxy.productmodel.form.ListAloneCourseProductForm;
import com.woniuxy.productmodel.form.UpdateAloneCourseForm;
import com.woniuxy.productservice.exception.ProductException;
import com.woniuxy.productservice.service.AloneCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class AloneCourseServiceImpl extends ServiceImpl<AloneCourseMapper, AloneCourse> implements AloneCourseService {

    @Autowired
    private CoachFeignClient coachFeignClient;
    @Autowired
    private CourseFeignClient courseFeignClient;

    /**
     * 新增私教产品
     * @param form
     * @param venueId
     * @return
     */
    @Override
    public AloneCourseProductDto addAloneCourseProduct(AddAlongCourseProductForm form, Integer venueId) {
        //同一教练,只能添加一种课程类型的私教产品
        AloneCourse course = getOne(new LambdaQueryWrapper<AloneCourse>().eq(AloneCourse::getCoachId, form.getCoachId()));
        if (!ObjectUtils.isEmpty(course) && course.getCourseTypeId()==form.getCourseTypeId())
            throw new ProductException(50003,"该教练已添加了该课程类型的私教产品");
        //教练是不是该场馆的教练，该教练是不是已经添加成私教产品了
        Result<CoachInfoDto> res1 = coachFeignClient.findCoachByCoachId(form.getCoachId());
        if (!res1.getFlag()) throw new ProductException(res1.getCode(),res1.getMsg());
        CoachInfoDto coachInfo = res1.getData();
        if (ObjectUtils.isEmpty(coachInfo)) throw new ProductException(50001,"教练不存在");
        if (venueId!=coachInfo.getVenueId()) throw new ProductException(50002,"教练不属于该场馆");
        if(coachInfo.getCoachState()!= CoachState.IS_ENABLE.getState()) throw new ProductException(50005,"教练未启用");
        //判断课程类别是否存在
        Result<CourseTypeDto> res2 = courseFeignClient.findCourseTypeById(form.getCourseTypeId());
        if (!res2.getFlag()) throw new ProductException(res2.getCode(),res2.getMsg());
        CourseTypeDto courseType = res2.getData();
        if (ObjectUtils.isEmpty(courseType)) throw new ProductException(50006,"该课程类别不存在");
        if (courseType.getCourseTypeState()!= CourseTypeStateEnum.TYPE_ENABLE.getCode()) throw new ProductException(50007,"课程类别未启用");
        //新增私教产品
        AloneCourse aloneCourse = BeanUtil.toBean(form, AloneCourse.class);
        aloneCourse.setCourseTypeName(courseType.getCourseTypeName());
        aloneCourse.setCoachAvatar(coachInfo.getCoachAvatar());
        aloneCourse.setCoachName(coachInfo.getCoachName());
        aloneCourse.setProductState(ProductState.IS_ENABLE.getState());
        aloneCourse.setVenueId(venueId);
        save(aloneCourse);
        AloneCourseProductDto dto = BeanUtil.toBean(aloneCourse, AloneCourseProductDto.class);
        return dto;
    }

    /**
     * 条件查询产品信息
     * @param form
     * @return
     */
    @Override
    public List<AloneCourseProductDto> listAloneCourseProduct(ListAloneCourseProductForm form) {
        List<AloneCourse> aloneCourseList = new LambdaQueryChainWrapper<AloneCourse>(getBaseMapper())
                .eq(!ObjectUtils.isEmpty(form.getCourseTypeId()), AloneCourse::getCourseTypeId, form.getCourseTypeId())
                .like(!ObjectUtils.isEmpty(form.getCoachName()), AloneCourse::getCoachName, form.getCoachName())
                .orderByAsc(AloneCourse::getCoachId).list();
        return BeanUtil.copyToList(aloneCourseList,AloneCourseProductDto.class);
    }

    /**
     * 下架私教产品
     * @param venueId
     * @param productId
     */
    @Override
    public void disabledProduct(Integer venueId, Integer productId) {
        AloneCourse aloneCourse = getById(productId);
        if (ObjectUtils.isEmpty(aloneCourse)) throw new ProductException(50002,"产品不存在");
        if (aloneCourse.getVenueId()!=venueId) throw new ProductException(50003,"不是该家的产品");
        if (aloneCourse.getProductState()==ProductState.IS_DISABLED.getState()) throw new ProductException(50004,"产品已下架，重复操作");
        aloneCourse.setProductState(ProductState.IS_DISABLED.getState());
        updateById(aloneCourse);
    }

    /**
     * 上架私教产品
     * @param venueId 场馆id
     * @param productId 产品id
     */
    @Override
    public void enableProduct(Integer venueId, Integer productId) {
        AloneCourse aloneCourse = getById(productId);
        if (ObjectUtils.isEmpty(aloneCourse)) throw new ProductException(50002,"产品不存在");
        if (aloneCourse.getVenueId()!=venueId) throw new ProductException(50003,"不是该家的产品");
        if (aloneCourse.getProductState()==ProductState.IS_ENABLE.getState()) throw new ProductException(50004,"产品已上架，重复操作");
        aloneCourse.setProductState(ProductState.IS_ENABLE.getState());
        updateById(aloneCourse);
    }

    /**
     * 商家端修改私教产品信息
     * @param form
     * @param venueId
     */
    @Override
    public void updateProduct(UpdateAloneCourseForm form, Integer venueId) {
        AloneCourse aloneCourse = getById(form.getCoachProductId());
        if (ObjectUtils.isEmpty(aloneCourse)) throw new ProductException(50002,"产品不存在");
        if (aloneCourse.getVenueId()!=venueId) throw new ProductException(50003,"不是该家的产品");
        aloneCourse.setCoachRemark(form.getCoachRemark());
        aloneCourse.setCoachProductCount(form.getCoachProductCount());
        aloneCourse.setCoachDuration(form.getCoachDuration());
        aloneCourse.setCoachProductPrice(form.getCoachProductPrice());
        updateById(aloneCourse);
    }


}
